package com.dlc.beijingrfid.adapter;

import android.view.View;
import android.widget.Button;
import com.dlc.commonlibrary.adapter.BaseRecyclerAdapter;
import com.dlc.beijingrfid.R;
import com.dlc.beijingrfid.model.intfc.ProductStatusList;
import com.dlc.beijingrfid.net.bean.RFIDListBean;

/**
 * Created by John on 2017/7/19.
 */

public class ProductStatusListAdapter<T extends RFIDListBean.DataBean.ListBean> extends BaseRecyclerAdapter<T> {

    private UnbindListener mUnbindListener;

    private final boolean isStorekeeper;

    public ProductStatusListAdapter(boolean isStorekeeper) {
        this.isStorekeeper = isStorekeeper;
    }

    /**
     * 解绑监听器
     */
    public interface UnbindListener {
        void onUnbind(int position);
    }

    @Override
    public int getItemLayoutId(int viewType) {
        return R.layout.item_product_status_list;
    }

    @Override
    public void onBindViewHolder(CommonHolder holder, int position) {
        RFIDListBean.DataBean.ListBean item = getItem(position);
        holder.setText(R.id.tv_rfid, item.getRFID());
        holder.setText(R.id.tv_name, item.getName());
        holder.setText(R.id.tv_spec, item.getSpec());
        holder.setText(R.id.tv_where, item.getWhere());
        Button btnUnbind = holder.getView(R.id.btn_unbind);
        if (isStorekeeper) {
            if ("2".equals(item.getStatus())) {
                btnUnbind.setEnabled(false);
                btnUnbind.setText(R.string.already_unbound);
            } else {
                // 只有还没解绑的，才能操作
                btnUnbind.setEnabled(true);
                btnUnbind.setText(R.string.unbundling_rfid);
            }
            btnUnbind.setVisibility(View.VISIBLE);
        } else {
            btnUnbind.setVisibility(View.GONE);
        }
    }

    @Override
    public void setChildViewListener(final CommonHolder holder, int viewType) {

        if (mUnbindListener != null) {
            Button btnUnbind = holder.getView(R.id.btn_unbind);
            btnUnbind.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mUnbindListener.onUnbind(holder.getAdapterPosition());
                }
            });
        }
    }

    public void setUnbindListener(UnbindListener unbindListener) {
        mUnbindListener = unbindListener;
    }
}
